package com.boot.utils;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;

public class RandomNumberUtil {

    public static void main(String[] args) throws ParseException {
        // splitRedPacket(8610, 90, 60, 180);
//        for(int i = 0; i < 100; i ++){
//            Random random = new Random();
//            int n = random.nextInt(18);
//            System.out.println("n= " + n);
//        }
//        LocalDate today = LocalDate.now();
//        for(long i = 0;i <= 12; i++){
//            LocalDate localDate = today.minusMonths(i);
//            String month = localDate.toString().substring(0,7);
//            System.out.println("month:" + month);
//        }
//        for(long i = 0; i < 100; i++) {
//            int productRandom = 100 + (int) (Math.random() * (1000 + 1 - 1));
//            System.out.println(productRandom);
//        }

//        int num = (int) (3/(float)3*100);
//        System.out.println(num);

        Random rn = new Random();
        String j = "199.9";
        String[] strings = j.split("\\.");
        for(int i = 0; i < 200; i++){
            int a = random.nextInt(8 - 6 + 1) + 6;
            int s = rn.nextInt(9) + 1;
            String ss = strings[0] + "." + a + s;
            System.out.println(ss);
        }

//        BigDecimal profit = new BigDecimal(0.00);
//        if(profit.doubleValue() == 0){
//            profit = new BigDecimal(0.01).setScale(2, BigDecimal.ROUND_DOWN);
//        }
//        //System.out.println(profit);
//
//        BigDecimal profit0 = new BigDecimal(0.01);
//
//        BigDecimal profit1 = new BigDecimal(80);
//
//        BigDecimal profit2 = new BigDecimal(99.9);
//
//        BigDecimal profit3 = new BigDecimal(199.99);
//
//        BigDecimal profit4 = new BigDecimal(369);
//
//        BigDecimal profit5 = new BigDecimal(3690);
//
//        BigDecimal profit6 = new BigDecimal(36900);
//
//        String s = String.format("%.2f", profit0).replace(".", "");
//        System.out.println(s);
//        if(s.length() < 13){
//            if(s.length()==3){
//                String s1 = "000000000" + s;
//                System.out.println(s1);
//            }
//        }
//
//        System.out.println(String.format("%.2f", profit1));
//        System.out.println(String.format("%.2f", profit2));
//        System.out.println(String.format("%.2f", profit3));
//        System.out.println(String.format("%.2f", profit4));
//        System.out.println(String.format("%.2f", profit5));
//        System.out.println(String.format("%.2f", profit6));


//        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
//        String s = "2020-07-15";
//
//        String minDateStr = "";
//        Calendar calc =Calendar.getInstance();
//        calc.setTime(df.parse(s));
//        //当前日期一个月后 
//        calc.add(Calendar.MONTH, 1);
//        Date minDate = calc.getTime();
//        minDateStr = df.format(minDate);
//        System.out.println(minDateStr);

    }

    /**
     * 随机数
     * @param total
     * @param splitCount
     * @param min
     * @param max
     */
    public static List splitRedPacket(double total, int splitCount, int min, int max) {
        System.out.println("总金额：	" + total);
        System.out.println("个数：	" + splitCount);
        System.out.println("最小金额：	" + min);
        System.out.println("最大金额：	" + max);

        List<Double> al = new ArrayList<>();
        if ((splitCount & 1) == 1) { // 奇数个红包，需要单独将其中一个红包先生成，以保证后续算法拆分份数为偶数。
            //System.out.println("红包个数" + splitCount + "是奇数，单独生成一个红包");
            double num = 0;
            do {
                num =rand(min,max,2); //random.nextInt(max);
                // 将后面算法拆分时的余数加入到这个随机值中。
                //System.out.println("单个的随机红包为：" + num);
            } while (num >= max || num <= min);

            total = total - num;
            al.add(num);
        }
        int couples = splitCount >> 1;
        double perCoupleSum = total / couples;

        if ((splitCount & 1) == 1) {
            //System.out.println("处理后剩余的金额为：" + total);
        }
        //System.out.println("将" + total + "元拆分为" + couples + "对金额，每对总额：" + perCoupleSum);
        for (int i = 0; i < couples; i++) {
            double num1;
            double num2;
            int index = 0;
            do {
                num1 = rand(min, max, 2);
                num2 = perCoupleSum - num1;
                if (!al.contains(num1) && !al.contains(num2)) {
                    if (i == 0) {
                        num1 = (total - couples * perCoupleSum) + num1;
                    }
                }
                index++;
                //System.err.println("////////////////////////////////////////////////////////////////////////////");
                //System.err.println("---------------------------开始分----------------------------");
                //System.err.println("---------------------------第几次分：" + index);
                //System.err.println("num1= " + num1);
                //System.err.println("num2= " + num2);
                //System.err.println("---------------------------当前分完----------------------------");
                //System.err.println("隔开");
            } while (num1 < min || num1 > max || num2 < min || num2 > max);
            if (num1 >= min && num1 <= max && num2 >= min && num2 <= max) {
                al.add(num1);
                al.add(num2);
            }
        }

        //double check_num = 0;
        //for (Double x : al) {
            //check_num = check_num + x;
            //DecimalFormat df = new DecimalFormat("#.00");
            //System.err.println("生成的金额: "+df.format(x));
        //}
        //System.out.println("总和：" + check_num);
        return al;
    }

    private final static Random random = new Random();

    /**
     * 获取随机数(double)
     *
     * @param min
     *            最小值
     * @param max
     *            最大值
     * @param round
     *            保留多少位小数（不能太大）
     * @return Double, null: when max < min
     */
    public static final Double rand(double min, double max, int round) {
        double dRound = Math.pow(10, round);
        int iMin = (int) (min * dRound);
        int iMax = (int) (max * dRound);
        Integer r = rand(iMin, iMax);
        if (r == null) {
            return null;
        }
        return r / dRound;
    }

    /**
     * 获取随机数
     *
     * @param min
     * @param max
     * @return Integer, null: when max < min
     */
    public static final Integer rand(int min, int max) {
        int tmp = max - min;
        if (tmp < 0) {
            return null;
        } else if (tmp == 0) {
            return min;
        } else {
            return random.nextInt(tmp + 1) + min;
        }
    }

    public String verificationCode(){
        int amount = (int)((Math.random() * 9 + 1) * 100000);
        String numberCode = String.valueOf(amount);
        return numberCode;
    }

}
